﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

using iHouse.Model.Cache;

namespace iHouse.Web.Infrastructure
{
    public class DataContextProvider : IDataContextProvider
    {
        #region IDataContextProvider Members

        public void RegisterDataContext<T>(string key, string connectionString)
        {
            if (System.Web.HttpContext.Current != null)
            {
                var dataContext = string.IsNullOrEmpty(connectionString)
                ? (T)Activator.CreateInstance(typeof(T))
                : (T)Activator.CreateInstance(typeof(T), new object[] { connectionString });

                System.Web.HttpContext.Current.Items[key] = dataContext;
            }
        }

        public T GetDataContext<T>(string key)
        {
            return (T)System.Web.HttpContext.Current.Items[key];
        }

        #endregion
    }
}
